package com.flyme.module.base.provider.service.impl;

import com.flyme.common.core.exception.OpenMessageException;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.base.client.constants.BaseConstants;
import com.flyme.module.base.client.entity.SystemAction;
import com.flyme.module.base.provider.mapper.SysActionMapper;
import com.flyme.module.base.provider.service.SysActionService;
import com.flyme.module.base.provider.service.SysAuthorizeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author zyf
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class SysActionServiceImpl extends BaseServiceImpl<SysActionMapper, SystemAction> implements SysActionService {

    @Autowired
    private SysAuthorizeService systemAccessService;



    /**
     * 检查Action编码是否存在
     *
     * @param actionCode
     * @return
     */
    @Override
    public Boolean isExist(String actionCode) {
        int count = count(q().eq("actionCode",actionCode));
        return count > 0;
    }

    /**
     * 添加Action操作
     *
     * @param action
     * @return
     */
    @Override
    public Boolean addAction(SystemAction action) {
        if (isExist(action.getActionCode())) {
            throw new OpenMessageException(String.format("%sAction编码已存在,不允许重复添加", action.getActionCode()));
        }
        if (action.getMenuId() == null) {
            action.setMenuId(0L);
        }
        if (action.getPriority() == null) {
            action.setPriority(0);
        }
        if (action.getStatus() == null) {
            action.setStatus(BaseConstants.ENABLED);
        }
        if (action.getIsPersist() == null) {
            action.setIsPersist(BaseConstants.DISABLED);
        }
        action.setCreateTime(new Date());
        action.setUpdateTime(action.getCreateTime());
        return  save(action);
    }

    /**
     * 修改Action操作
     *
     * @param action
     * @return
     */
    @Override
    public Boolean updateAction(SystemAction action) {
        SystemAction savedAction = getById(action.getId());
        if (savedAction == null) {
            throw new OpenMessageException(String.format("%sAction不存在", action.getId()));
        }
        if (!savedAction.getActionCode().equals(action.getActionCode())) {
            // 和原来不一致重新检查唯一性
            if (isExist(action.getActionCode())) {
                throw new OpenMessageException(String.format("%sAction编码已存在,不允许重复添加", action.getActionCode()));
            }
        }
        if (action.getMenuId() == null) {
            action.setMenuId(0L);
        }
        if (action.getPriority() == null) {
            action.setPriority(0);
        }
        action.setUpdateTime(new Date());
        boolean result = saveOrUpdate(action);
        // 同步授权表里的信息
        systemAccessService.updateGrantAccess(BaseConstants.RESOURCE_TYPE_ACTION, action.getId());
        return result;
    }

    /**
     * 更新启用禁用
     *
     * @param actionId
     * @param status
     * @return
     */
    @Override
    public Boolean updateStatus(Long actionId, Integer status) {
        SystemAction action = new SystemAction();
        action.setId(actionId);
        action.setStatus(status);
        action.setUpdateTime(new Date());
        Boolean count =saveOrUpdate(action);
        // 同步授权表里的信息
        systemAccessService.updateGrantAccess(BaseConstants.RESOURCE_TYPE_ACTION, action.getId());
        return count;
    }

    /**
     * 移除Action
     *
     * @param actionId
     * @return
     */
    @Override
    public Boolean removeAction(Long actionId) {
        SystemAction action = getById(actionId);
        if (action != null && action.getIsPersist().equals(BaseConstants.ENABLED)) {
            throw new OpenMessageException(String.format("保留数据,不允许删除"));
        }
        if (systemAccessService.isExist(actionId, BaseConstants.RESOURCE_TYPE_ACTION)) {
            throw new OpenMessageException(String.format("资源已被授权,不允许删除,请取消授权后,再次尝试!"));
        }
        return removeById(actionId);
    }


}
